package com.macro.mall.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.macro.mall.common.enums.DelFlagEnums;
import com.macro.mall.common.enums.SitQuietlyClockFieldEnum;
import com.macro.mall.common.service.RedisService;
import com.macro.mall.dto.admin.*;
import com.macro.mall.mapper.SitQuietlyClockMapper;
import com.macro.mall.mapper.UmsMemberMapper;
import com.macro.mall.model.SitQuietlyClock;
import com.macro.mall.service.SitQuietlyClockService;
import com.macro.mall.vo.admin.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author xq
 * @description SitQuietlyClockServiceImpl
 * @date 2024/2/20 21:13
 */
@Slf4j
@Service
public class SitQuietlyClockServiceImpl implements SitQuietlyClockService {

    @Resource
    private SitQuietlyClockMapper sitQuietlyClockMapper;

    @Resource
    private RedisService redisService;

    @Resource
    private UmsMemberMapper umsMemberMapper;

    @Override
    public void addClock(AdminClockAddDTO req) {
        String year = DateUtil.format(req.getSignDate(), DatePattern.NORM_YEAR_PATTERN);
        String month = DateUtil.format(req.getSignDate(), "MM");
        String day = DateUtil.format(req.getSignDate(), "dd");
        Long statDateNo = Long.valueOf(DateUtil.format(req.getSignDate(), DatePattern.PURE_DATE_PATTERN));
        SitQuietlyClock sitQuietlyClock = new SitQuietlyClock();
        sitQuietlyClock.setType(req.getType());
        sitQuietlyClock.setUserId(req.getClockMemberId());
        sitQuietlyClock.setStartTime(DateUtil.offsetMinute(DateUtil.date(), -req.getTimeInterval()));
        sitQuietlyClock.setEndTime(DateUtil.date());
        sitQuietlyClock.setTimeInterval(req.getTimeInterval());
        sitQuietlyClock.setDateNo(statDateNo);
        sitQuietlyClock.setYear(year);
        sitQuietlyClock.setMonth(month);
        sitQuietlyClock.setDay(day);
        sitQuietlyClock.setSignDate(DateUtil.format(req.getSignDate(), DatePattern.NORM_DATE_PATTERN));
        sitQuietlyClock.setDelFlag(DelFlagEnums.UNDELETED.getNo());
        sitQuietlyClock.setModifyTime(new Date());
        sitQuietlyClock.setCreateTime(new Date());
        sitQuietlyClockMapper.insertSelective(sitQuietlyClock);
    }

    @Override
    public void updateClock(AdminClockUpdateDTO req) {
        String year = DateUtil.format(req.getSignDate(), DatePattern.NORM_YEAR_PATTERN);
        String month = DateUtil.format(req.getSignDate(), "MM");
        String day = DateUtil.format(req.getSignDate(), "dd");
        Long statDateNo = Long.valueOf(DateUtil.format(req.getSignDate(), DatePattern.PURE_DATE_PATTERN));
        SitQuietlyClock sitQuietlyClock = new SitQuietlyClock();
        sitQuietlyClock.setId(req.getId());
        sitQuietlyClock.setType(req.getType());
        sitQuietlyClock.setStartTime(DateUtil.offsetMinute(DateUtil.date(), -req.getTimeInterval()));
        sitQuietlyClock.setEndTime(DateUtil.date());
        sitQuietlyClock.setTimeInterval(req.getTimeInterval());
        sitQuietlyClock.setDateNo(statDateNo);
        sitQuietlyClock.setYear(year);
        sitQuietlyClock.setMonth(month);
        sitQuietlyClock.setDay(day);
        sitQuietlyClock.setSignDate(DateUtil.format(req.getSignDate(), DatePattern.NORM_DATE_PATTERN));
        sitQuietlyClock.setDelFlag(DelFlagEnums.UNDELETED.getNo());
        sitQuietlyClock.setModifyTime(DateUtil.date());
        sitQuietlyClockMapper.updateByPrimaryKeySelective(sitQuietlyClock);

    }

    @Override
    public void deleteClock(Long id) {
        SitQuietlyClock sitQuietlyClock = new SitQuietlyClock();
        sitQuietlyClock.setId(id);
        sitQuietlyClock.setDelFlag(DelFlagEnums.DELETED.getNo());
        sitQuietlyClock.setModifyTime(DateUtil.date());
        sitQuietlyClockMapper.updateByPrimaryKeySelective(sitQuietlyClock);

    }


    @Override
    public List<AdminSitQuietlyClockVO> list(AdminClockQueryDTO req) {
        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        List<AdminSitQuietlyClockVO> res = sitQuietlyClockMapper.sitQuietlyList(req);
        PageHelper.clearPage();
        return res;
    }

    @Override
    public List<AdminUnClockListVO> unClockList(AdminUnClockListDTO req) {
        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        List<AdminUnClockListVO> res = sitQuietlyClockMapper.unClockList(req);
        PageHelper.clearPage();
        return res;

    }

    @Override
    public List<String> preClockList(AdminPreClockDTO req) {
        // 取昨天
        Date yesterday = DateUtils.addDays(DateUtil.date(), -1);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(yesterday);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        Date endTime = calendar.getTime();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date startTime = calendar.getTime();
        req.setStartTimeDateNo(Long.valueOf(DateUtil.format(startTime, DatePattern.PURE_DATE_PATTERN)));
        req.setEndTimeDateNo(Long.valueOf(DateUtil.format(endTime, DatePattern.PURE_DATE_PATTERN)));
        return sitQuietlyClockMapper.preClockList(req);
    }

    @Override
    public void processUnClockTask() {
        // 1.查询某天未打卡用户记录
        Date yesterdayDate = DateUtils.addDays(DateUtil.date(), -1);
        Long statDateNo = Long.valueOf(DateUtil.format(DateUtils.addDays(DateUtil.date(), -1), DatePattern.PURE_DATE_PATTERN));
        AdminPreClockDTO adminPreClockDTO = new AdminPreClockDTO();
        adminPreClockDTO.setStartTimeDateNo(statDateNo);
        adminPreClockDTO.setEndTimeDateNo(statDateNo);
        List<Long> unClockUserIds = sitQuietlyClockMapper.preClockUserIdList(adminPreClockDTO);
        if (ObjectUtil.isEmpty(unClockUserIds)) {
            return;
        }
        String year = DateUtil.format(yesterdayDate, DatePattern.NORM_YEAR_PATTERN);
        String month = DateUtil.format(yesterdayDate, "MM");
        String day = DateUtil.format(yesterdayDate, "dd");
        for (Long userid : unClockUserIds) {
            SitQuietlyClock sitQuietlyClock = new SitQuietlyClock();
            sitQuietlyClock.setType(SitQuietlyClockFieldEnum.TYPE.UN_CLOCK.getNo());
            sitQuietlyClock.setUserId(userid);
            sitQuietlyClock.setStartTime(yesterdayDate);
            sitQuietlyClock.setEndTime(yesterdayDate);
            sitQuietlyClock.setTimeInterval(0);
            sitQuietlyClock.setDateNo(statDateNo);
            sitQuietlyClock.setYear(year);
            sitQuietlyClock.setMonth(month);
            sitQuietlyClock.setDay(day);
            sitQuietlyClock.setSignDate(DateUtil.format(yesterdayDate, DatePattern.NORM_DATE_PATTERN));
            sitQuietlyClock.setDelFlag(DelFlagEnums.UNDELETED.getNo());
            sitQuietlyClock.setModifyTime(DateUtil.date());
            sitQuietlyClock.setCreateTime(DateUtil.date());
            sitQuietlyClockMapper.insertSelective(sitQuietlyClock);
        }
    }

    @Override
    public AdminPreClockDataVO preClockDataVO(AdminPreClockDTO req) {
        Long statDateNo = Long.valueOf(DateUtil.format(DateUtils.addDays(DateUtil.date(), -1), DatePattern.PURE_DATE_PATTERN));
        Integer allCount = umsMemberMapper.countKlyxNum(req.getSourceType());
        Integer clockNum = sitQuietlyClockMapper.clockNumByType(statDateNo, SitQuietlyClockFieldEnum.TYPE.CLOCK.getNo(), req.getSourceType());
        Integer applyNum = sitQuietlyClockMapper.clockNumByType(statDateNo, SitQuietlyClockFieldEnum.TYPE.APPLY.getNo(), req.getSourceType());
        AdminPreClockDataVO res = new AdminPreClockDataVO();
        res.setClockNum(clockNum);
        res.setApplyNum(applyNum);
        res.setUnClockNum(allCount - clockNum - applyNum);
        res.setAllCount(allCount);
        return res;

    }

    @Override
    public List<AdminUserNameVO> listUserNames(Integer type) {
        // 存 redis里面获取。
        return sitQuietlyClockMapper.listUserNames(type);
    }

    @Override
    public List<UnClockUserVO> listUnClock(Integer type) {
        // 获取当前月时间
        Date beginDate = DateUtil.beginOfMonth(DateUtil.date());
        Date endDate = DateUtil.endOfMonth(DateUtil.date());

        Long statDateNo = Long.valueOf(DateUtil.format(beginDate, DatePattern.PURE_DATE_PATTERN));
        Long endDateNo = Long.valueOf(DateUtil.format(endDate, DatePattern.PURE_DATE_PATTERN));

        List<UnClockUserVO> res = sitQuietlyClockMapper.listUnClock(statDateNo, endDateNo, type);
        return res;
    }
}
